package com.itcast.demoone.kotlin

fun main() {
   fn02()
}

fun fn01(){
    val numbers = listOf(1, 2, 3, 4, 5)

    // 过滤 - 找出偶数
    val evens = numbers.filter { it % 2 == 0 }  // [2, 4]

    // 映射 - 每个元素加倍
    val doubled = numbers.map { it * 2}  // [2, 4, 6, 8, 10]

    // 查找 - 找到第一个大于3的元素
    val firstLarge = numbers.first { it > 3 }  // 4

    println("$evens, $doubled, $firstLarge")

    // 遍历 - 打印元素
    numbers.forEach{ println("Number: $it") }
}

fun fn02(){
    fun calculate(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int {
        return operation(num1, num2)
    }

    val res1 = calculate(10, 20) { a, b -> a + b }
    println(res1)

    val res2 = calculate(10, 20) { a, b -> a * b }
    println(res2)
}